Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { allBlogs } from 'content-collections'
3import { SITE_DESCRIPTION, SITE_TITLE, SITE_URL } from '#/lib/site'
4
5export const Route = createFileRoute('/rss.xml')({
6 server: {
7 handlers: {
8 GET: () => {
9 const posts = Array.from(
10 new Map(
11 [...allBlogs]
12 .sort(
13 (a, b) =>
14 new Date(b.pubDate).valueOf() - new Date(a.pubDate).valueOf(),
15 )
16 .map((post) => [post.slug, post]),
17 ).values(),
18 )
19
20 const items = posts
21 .map((post) => {
22 const url = `${SITE_URL}/blog/${post.slug}`
23 return `<item><title><![CDATA[${post.title}]]></title><description><![CDATA[${post.description}]]></description><link>${url}</link><guid>${url}</guid><pubDate>${new Date(post.pubDate).toUTCString()}</pubDate></item>`
24 })
25 .join('')
26
27 const xml = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title><![CDATA[${SITE_TITLE}]]></title><description><![CDATA[${SITE_DESCRIPTION}]]></description><link>${SITE_URL}</link>${items}</channel></rss>`
28
29 return new Response(xml, {
30 headers: {
31 'Content-Type': 'application/rss+xml; charset=utf-8',
32 },
33 })
34 },
35 },
36 },
37})